package com.hwh.lib.view.itemDecoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.hwh.lib.R


/**
 * 文件描述：
 * 作者：hwh
 * 创建时间：2021/6/19
 *
 */
class ProgressItemDecoration(private val context: Context) : RecyclerView.ItemDecoration() {
    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var linePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var radius = 0f
    private var curPosition = 0 // 当前进行中的位置


    init {
        radius = dp2Px(8)
        circlePaint.run {
            color = context.resources.getColor(R.color.colorPrimary)
            style = Paint.Style.FILL
            strokeWidth = dp2Px(2)
        }
        linePaint.run {
            color = context.resources.getColor(R.color.colorPrimary)
            strokeWidth = dp2Px(2)
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.run {
            top = dp2Px(20).toInt()
            left = dp2Px(50).toInt()
            right = dp2Px(20).toInt()
        }
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)
        val childCount = parent.childCount
        val layoutManager = parent.layoutManager
        for (i in 0 until childCount) {
            val childView: View = parent.getChildAt(i)
            val leftDecorationWidth = layoutManager!!.getLeftDecorationWidth(childView)
            val topDecorationHeight = layoutManager.getTopDecorationHeight(childView)
            // 获取当前 item 是 recyclerview 的第几个 childview
            val childLayoutPosition = parent.getChildLayoutPosition(childView)
            val startX = (leftDecorationWidth / 2).toFloat()
            val stopX = startX
            // 圆顶部部分竖线，起点 Y
            val topStartY: Float = (childView.top - topDecorationHeight).toFloat()
            // 圆顶部部分竖线，终点 Y
            val topStopY: Float = childView.top + childView.height / 2 - radius

            // 圆底部部分竖线，起点 Y
            val bottomStartY: Float = childView.top + childView.height / 2 + radius
            // 圆底部部分竖线，终点 Y
            val bottomStopY: Float = childView.bottom.toFloat()

            // 位置超过 curPosition 时，竖线颜色设置为浅色
            if (childLayoutPosition > curPosition) {
                linePaint.color = context.resources.getColor(R.color.colorPrimaryTint)
                circlePaint.color = context.resources.getColor(R.color.colorPrimaryTint)
                circlePaint.style = Paint.Style.STROKE
            } else {
                linePaint.color = context.resources.getColor(R.color.colorPrimary)
                circlePaint.color = context.resources.getColor(R.color.colorPrimary)
                circlePaint.style = Paint.Style.FILL
            }

            // 绘制圆
            if (childLayoutPosition == curPosition) {
                circlePaint.style = Paint.Style.STROKE
                canvas.drawCircle(
                    leftDecorationWidth / 2f,
                    childView.top + childView.height / 2f,
                    dp2Px(2),
                    circlePaint
                )
            }
            canvas.drawCircle(
                leftDecorationWidth / 2f,
                childView.top + childView.height / 2f,
                radius,
                circlePaint
            )

            // 绘制竖线 , 第 0 位置上只需绘制 下半部分
            when (childLayoutPosition) {
                0 -> {
                    // 当前 item position = curPosition 时，绘制下半部分竖线时，颜色设置为浅色
                    if (childLayoutPosition == curPosition) {
                        linePaint.color = context.resources.getColor(R.color.colorPrimaryTint)
                    }
                    canvas.drawLine(startX, bottomStartY, startX, bottomStopY, linePaint)
                    // 最后位置上，只需绘制上半部分
                }
                parent.adapter!!.itemCount - 1 -> {
                    canvas.drawLine(startX, topStartY, startX, topStopY, linePaint)
                }
                else -> {
                    // 都要绘制
                    canvas.drawLine(startX, topStartY, startX, topStopY, linePaint)
                    // 当前 item position = curPosition 时，绘制下半部分竖线时，颜色设置为浅色
                    if (childLayoutPosition == curPosition) {
                        linePaint.color = context.resources.getColor(R.color.colorPrimaryTint)
                    }
                    canvas.drawLine(startX, bottomStartY, startX, bottomStopY, linePaint)
                }
            }
        }
    }

    /**
     * 设置进行中的位置
     *
     * @param recyclerView
     * @param position
     */
    fun setDoingPosition(recyclerView: RecyclerView?, position: Int) {
        requireNotNull(recyclerView) { "RecyclerView can't be null" }
        requireNotNull(recyclerView.adapter) { "RecyclerView Adapter can't be null" }
        require(position >= 0) { "position can't be less than 0" }
        recyclerView.layoutManager!!.itemCount
        require(position <= recyclerView.adapter!!.itemCount - 1) { "position can't be greater than item count" }
        curPosition = position
    }

    private fun dp2Px(value: Int): Float = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        value.toFloat(),
        context.resources.displayMetrics
    )

}